package com.controller;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Company;
import com.beans.Msg;
import com.beans.MsgReply;
import com.beans.User;
import com.service.MsgManager;
import com.vo.Page;

public class MsgController extends UniversalController {
	private MsgManager msgManager;

	public MsgManager getMsgManager() {
		return msgManager;
	}

	public void setMsgManager(MsgManager msgManager) {
		this.msgManager = msgManager;
	}

	public ModelAndView getDelete(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		int id = 0;
		String msg = "";
		try {
			id = Integer.parseInt(request.getParameter("id"));// ;id=12
			Msg m = (Msg) getUniversalManager().findByID(com.beans.Msg.class,
					id);
			getUniversalManager().delete(m);
			msg = "删除成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败！";
		}
		getMessage(response, msg);
		return null;
	}

	public ModelAndView getShowMsgList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map param = new HashMap();
		Map result = new HashMap();
		Msg msg = new Msg();
		bind(request, msg);
		Page page = msgManager.getMsgDao().list(this.getPageNo(request),
				pageSize, msg);
		result.put("o", page);
		if (msg.getUserID() > 0)
			return new ModelAndView("user/myMsgList", result);
		else if (msg.getCompanyID() > 0)
			return new ModelAndView("company/myMsgList", result);
		else
			return new ModelAndView("msg/ShowMsgList", result);
	}
	public ModelAndView myMsgList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		Map result = new HashMap();
		Msg msg = new Msg();
		bind(request, msg);
		if (u!=null){
			msg.setUserID(u.getId());
			Page page = msgManager.getMsgDao().list(this.getPageNo(request),pageSize, msg);
			result.put("o", page);
			return new ModelAndView("user/myMsgList", result);
		}
		else if (c!=null){
			msg.setCompanyID(c.getId());
			Page page = msgManager.getMsgDao().list(this.getPageNo(request),pageSize, msg);
			result.put("o", page);
			return new ModelAndView("company/myMsgList", result);
		}
		return null;
	}
	public ModelAndView addMsg(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map param = new HashMap();
		Map result = new HashMap();
		Msg msg = new Msg();
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		if (u != null && c == null) {
			msg.setUserID(u.getId());
			msg.setUserName(u.getRealName());
			result.put("o", u);
			return new ModelAndView("msg/addMsg", result);
		}

		if (c != null) {
			msg.setCompanyID(c.getId());
			msg.setCompanyName(c.getcName());
			result.put("o", u);
			return new ModelAndView("msg/addMsg", result);
		}
		return new ModelAndView("login", result);
	}

	public ModelAndView getAddMsgReply(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		String hidden = request.getParameter("hidden");
		String hiddenName = request.getParameter("hiddenName");
		String code = (String) request.getParameter("code");
		String validateCode = (String) request.getSession().getAttribute(
				"validateCode");
		if (validateCode == null) {
			getMessage(response, "验证码过期！");
			return null;
		}
		if (null == code || "".equals(code)
				|| !code.toUpperCase().equals(validateCode.toUpperCase())) {
			getMessage(response, "验证码错误！");
			return null;
		}
		MsgReply m = new MsgReply();
		bind(request, m);
		if (m.getContent() == null || "".equals(m.getContent())) {
			getMessage(response, "请输入留言内容！");
			return null;
		}
		if (u != null) {
			m.setUserID(u.getId());
			if ("Y".equals(hidden))
				m.setUserName(hiddenName + "(匿名)");
			else
				m.setUserName(u.getRealName());
			getUniversalManager().insert(m);
			getMessage(response, "留言成功！");
			return null;
		} else if (c != null) {
			m.setCompanyID(c.getId());
			if ("Y".equals(hidden))
				m.setCompanyName(hiddenName + "(匿名)");
			else
				m.setCompanyName(c.getcName());
			getUniversalManager().insert(m);
			getMessage(response, "留言成功！");
			return null;
		}
		getMessage(response, "您还没有登录！请登录后发表留言。");
		return null;
	}

	public ModelAndView getShowMsg(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		String id = request.getParameter("id");
		try {
			Msg msg = (Msg) getUniversalManager().findByID(Msg.class,
					Integer.parseInt(id));
			result.put("o", msg);
			return new ModelAndView("msg/showMsg", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public ModelAndView insertMsg(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Msg msg = new Msg();
		User u = (User) request.getSession().getAttribute("user");
		Company c = (Company) request.getSession().getAttribute("company");
		bind(request, msg);
		msg.setInputdate(new Date());
		Date d = new Date();
		if (d.getMonth() < 11)
			d.setMonth(d.getMonth() + 1);
		else
			d.setYear(d.getYear() + 1);
		if (msg.getEnddate() == null)
			msg.setEnddate(d);
		result.put("msg", "添加成功！");
		try {
			if (u != null && c == null) {
				msg.setUserID(u.getId());
				msg.setUserName(u.getRealName());
				getUniversalManager().insert(msg);
				result.put("o", u);
				return new ModelAndView("msg/addMsg", result);
			}

			if (c != null) {
				msg.setCompanyID(c.getId());
				msg.setCompanyName(c.getcName());
				getUniversalManager().insert(msg);
				result.put("o", u);
				return new ModelAndView("msg/addMsg", result);
			}
		} catch (Exception e) {
			result.put("msg", "添加失败！");
			return new ModelAndView("msg/addMsg", result);
		}
		return null;
	}

	public ModelAndView msgReplyList(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		Map param = new HashMap();
		String pid = request.getParameter("pid");
		String tableName = request.getParameter("tableName");
		try {
			param.put("tableName", tableName);
			param.put("pid", Integer.parseInt(pid));
			Page page = getUniversalManager().findPageByCriteria(
					this.getPageNo(request), pageSize, param, MsgReply.class);
			result.put("o", page);
			return new ModelAndView("msg/msgReplyList", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
